import * as React from 'react';
import { Form, Input, Modal, Select, message, Radio } from 'antd';
import request from '@/http';
import qs from 'qs';

interface GNEditObject {
  isModalVisible: boolean;
  id?: number;
  team_name?: string;
  isGraduation?: number;
}

//项目管理 编辑小组
const GroupNameEdit = (props: { editObject: GNEditObject; onClose: any }) => {
  const { editObject, onClose } = props;
  const [isModalVisible, setIsModalVisible] = React.useState(false);
  const [formObject] = Form.useForm();
  const teamId = editObject.id;

  const modelOnOk = async (props: any) => {
    const data = await formObject.validateFields();
    console.log(data);
    const groupId = props.isModalVisible;

    const users = {
      ...data,
      teamId: teamId,
    };
    console.log(users);

    const result: any = await request.post(
      '/api/admin/group/UpdateTeam',
      qs.stringify(users),
    );
    console.log('编辑项目组:', result);
    if (result.code == 200) {
      setTimeout(() => {
        message.success('编辑成功');
      }, 200);

      if (onClose) {
        onClose();
      }
      setIsModalVisible(false);
    }
  };

  const modelOnCancel = () => {
    setIsModalVisible(false);
  };

  React.useEffect(() => {
    setIsModalVisible(editObject.isModalVisible);
    if (editObject.id !== null) {
      formObject.setFieldsValue({
        teamName: editObject.team_name,
      });
    }
  }, [editObject]);

  //单选框的默认值
  const [value, setValue] = React.useState(0);

  return (
    <>
      <Modal
        width={800}
        className="modal"
        title={'编辑小组'}
        visible={isModalVisible}
        onCancel={modelOnCancel}
        onOk={modelOnOk}
        maskClosable={false}
        destroyOnClose
        okText="确认"
        cancelText="取消"
      >
        <Form
          className="form"
          name="editForm"
          form={formObject}
          preserve={false}
        >
          <Form.Item label="小组名称" name="teamName">
            <Input className="addGroupInput" placeholder="请输入..." />
          </Form.Item>
          <Form.Item label="是否结业" name="isGraduation">
            <Radio.Group value={value}>
              <Radio value={1}>已结业</Radio>
              <Radio value={0}>未结业</Radio>
            </Radio.Group>
          </Form.Item>
        </Form>
      </Modal>
    </>
  );
};

export { GroupNameEdit, GNEditObject };
